
Imports std
Imports System.IO

Module deduplineW

    'Public Const hashbase As Int32 = 999999
    Public Const seperator As Int32 = 3
    'Private line As hashmap(Of String, UInt64) = New hashmap(Of String, UInt64)(AddressOf keyToIndex, hashbase)
    'Private line As map(Of String, UInt64) = New map(Of String, UInt64)
    'Private content As deque(Of String) = New deque(Of String)
    Private line As Dictionary(Of String, UInt64) = New Dictionary(Of String, UInt64)

    'Public Function keyToIndex(ByRef k As String) As Int32
    '    Dim i As Int32
    '    Dim rtn As Int32
    '    For i = 0 To strlen(k) - 1
    '        rtn <<= 4
    '        rtn += Convert.ToInt32(k(i))
    '    Next
    '    rtn = rtn Mod hashbase
    'End Function

    Public Sub parse(ByRef fileName As String)
        On Error Resume Next
        Dim reader As TextReader = Nothing
        Dim input As String = Nothing
        Dim rand As New System.Random()

        If fileName = "/-" Then
            reader = Console.In
        Else
            reader = New StreamReader(New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        End If
        assert(Not reader Is Nothing, "file no found, " + fileName)
        input = reader.ReadLine
        While Not input Is Nothing
            '    If rand.Next(10) < seperator And Not content.empty() Then
            '        If rand.Next(10) >= 5 Then
            '            copy(input, content.front.data)
            '            content.pop_front()
            '        Else
            '            copy(input, content.back.data)
            '            content.pop_back()
            '        End If
            '        line(input) += 1
            '    Else
            '        input = reader.ReadLine()
            '        kickNewlineChar(input)
            '        If Not emptyString(input) Then
            '            If rand.Next(10) >= seperator Then
            '                content.push_front(input)
            '            Else
            '                content.push_back(input)
            '            End If
            '            line(input) += 1
            '        End If
            '    End If
            'End While

            'closestream(reader)
            'While content.size() > 0
            '    If rand.Next(10) >= 5 Then
            '        input = content.front.data
            '        content.pop_front()
            '    Else
            '        input = content.back.data
            '        content.pop_back()
            '    End If
            '    line(input) += 1
            kickNewlineChar(input)
            If Not isemptystring(input) Then
                If Not line.ContainsKey(input) Then
                    line.Add(input, 0)
                End If
                line(input) += 1
            End If
            input = reader.ReadLine
        End While
        closestream(reader)
    End Sub

    Sub Main(ByVal args() As String)
        On Error Resume Next
        Dim writer As TextWriter = Nothing
        Dim writer2 As TextWriter = Nothing
        Dim i As Int32
        Dim baseline As Int32
        'Dim it As map(Of String, UInt64).node
        'Dim it As hashmap(Of String, UInt64).iterator = Nothing
        Dim it As Dictionary(Of String, UInt64).KeyCollection.Enumerator = Nothing

        'pause()
        If args.Length < 3 Then
            raiseError("argument count error, threshold input0 input1 ... inputN output," + _
                        "if input/output is /-, use console", errorHandle.errorType.critical)
        Else
            For i = 1 To args.Length() - 2
                parse(args(i))
            Next
            baseline = Convert.ToUInt64(args(0))
            If args(args.Length() - 1) = "/-" Then
                writer = Console.Out
            Else
                writer = New StreamWriter(New FileStream(args(args.Length - 1), FileMode.Create, FileAccess.Write, FileShare.Read))
                writer2 = New StreamWriter(New FileStream(args(args.Length - 1) + "_all.txt", FileMode.Create, FileAccess.Write, FileShare.Read))
                assert(Not writer2 Is Nothing, _
                        "file no found, " + args(args.Length - 1) + "_all.txt")
            End If
            assert(Not writer Is Nothing, _
                    "file no found, " + args(args.Length - 1))
            'it = line.begin()

            'While Not it Is line.end
            '    If it.value >= baseline Then
            '        writer.WriteLine(it.key)
            '    End If
            '    writer2.Write(it.key)
            '    writer2.Write(character.tab)
            '    writer2.WriteLine(it.value)
            '    it += 1
            'End While
            it = line.Keys.GetEnumerator
            While it.MoveNext
                If line(it.Current) >= baseline Then
                    writer.WriteLine(it.Current)
                End If
                If Not writer2 Is Nothing Then
                    writer2.Write(it.Current)
                    writer2.Write(character.tab)
                    writer2.WriteLine(line(it.Current))
                End If
            End While
            closestream(writer)
            closestream(writer2)
        End If
    End Sub

End Module
